/*
 * Main.java
 *
 * Created on 19/03/2012, 10:52:43
 */

package br.com.ucb.game8numbers.view.main;

import br.com.ucb.game8numbers.observer.DataResponsibleObserver;
import br.com.ucb.game8numbers.observer.subject.DataResponsibleSubject;
import br.com.ucb.game8numbers.service.DataResponsible;
import br.com.ucb.game8numbers.service.ServiceResolver;
import br.com.ucb.game8numbers.util.Utils;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

/**
 *
 * @author robs
 */
public class Main extends javax.swing.JFrame implements DataResponsibleObserver{

    DataResponsibleSubject dataResponsible;
    ServiceResolver service;
    /** Creates new form Main */
    public Main() {
        dataResponsible = new DataResponsible();
        dataResponsible.registerObserver(this);
        service = new ServiceResolver((DataResponsible)dataResponsible);
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        button1 = new javax.swing.JButton();
        button2 = new javax.swing.JButton();
        button3 = new javax.swing.JButton();
        button4 = new javax.swing.JButton();
        button5 = new javax.swing.JButton();
        button6 = new javax.swing.JButton();
        button7 = new javax.swing.JButton();
        button8 = new javax.swing.JButton();
        button9 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        totalMovimentos = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        menu = new javax.swing.JMenu();
        startIAResolver = new javax.swing.JMenuItem();
        reset = new javax.swing.JMenuItem();
        stopResolver = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        button1.setText("1");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        button2.setText("2");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        button3.setText("3");
        button3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button3ActionPerformed(evt);
            }
        });

        button4.setText("8");
        button4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button4ActionPerformed(evt);
            }
        });

        button5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button5ActionPerformed(evt);
            }
        });

        button6.setText("4");
        button6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button6ActionPerformed(evt);
            }
        });

        button7.setText("7");
        button7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button7ActionPerformed(evt);
            }
        });

        button8.setText("6");
        button8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button8ActionPerformed(evt);
            }
        });

        button9.setText("5");
        button9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button9ActionPerformed(evt);
            }
        });

        jLabel1.setText("Quantidade de movimentos executados:");

        totalMovimentos.setText("0");

        menu.setText("Menu");

        startIAResolver.setText("Resolver");
        startIAResolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                startIAResolverActionPerformed(evt);
            }
        });
        menu.add(startIAResolver);

        reset.setLabel("Reiniciar");
        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });
        menu.add(reset);

        stopResolver.setText("Stop Resolver");
        stopResolver.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stopResolverActionPerformed(evt);
            }
        });
        menu.add(stopResolver);

        jMenuBar1.add(menu);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(button7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button1, javax.swing.GroupLayout.DEFAULT_SIZE, 95, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(button8, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(button2, javax.swing.GroupLayout.DEFAULT_SIZE, 108, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(37, 37, 37)))
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button9, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(totalMovimentos)
                        .addComponent(button6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button3, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE)))
                .addContainerGap(19, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(58, 58, 58)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(button3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addComponent(button2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(button1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 76, Short.MAX_VALUE)))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(button6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(button5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(button4, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(button8, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                    .addComponent(button7, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE)
                    .addComponent(button9, javax.swing.GroupLayout.DEFAULT_SIZE, 71, Short.MAX_VALUE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(totalMovimentos))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
       //button 1 so pode mudar para o button 2 e o 4 
       if(getText(getButton2()).isEmpty()){
          changePosition(getButton1(), getButton2());
       }
       else if(getText(getButton4()).isEmpty())       {
          changePosition(getButton1(), getButton4());
       }
    }//GEN-LAST:event_button1ActionPerformed

    private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
       // button 2 so pode mudar para o button 1, 5, 3
       if(getText(getButton1()).isEmpty()){
           changePosition(getButton2(), getButton1());
       }
       else if(getText(getButton5()).isEmpty()){
           changePosition(getButton2(), getButton5());
       }
       else if(getText(getButton3()).isEmpty()){
           changePosition(getButton2(), getButton3());
       }
    }//GEN-LAST:event_button2ActionPerformed

    private void button5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button5ActionPerformed
       // button 5 so pode mudar para o button 2,4,6 e 8
        if(getText(getButton2()).isEmpty()){
            changePosition(getButton5(), getButton2());
        }
        else if(getText(getButton4()).isEmpty()){
            changePosition(getButton5(),getButton4());
        }
        else if(getText(getButton6()).isEmpty()){
            changePosition(getButton5(), getButton6());
        }else if(getText(getButton8()).isEmpty()){
            changePosition(getButton5(), getButton8());
        }
    }//GEN-LAST:event_button5ActionPerformed

    private void button3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button3ActionPerformed
       // button 3 so pode mudar para o button 2 e 6
        if(getText(getButton2()).isEmpty()){
            changePosition(getButton3(), getButton2());
        }
        else if(getText(getButton6()).isEmpty()){
            changePosition(getButton3(), getButton6());
        }
    }//GEN-LAST:event_button3ActionPerformed

    private void button4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button4ActionPerformed
        // button 4 so pode mudar para o button 1,5 e 7
        if(getText(getButton1()).isEmpty()){
            changePosition(getButton4(), getButton1());
        }
        else if(getText(getButton5()).isEmpty()){
            changePosition(getButton4(),getButton5());
        } 
        else if(getText(getButton7()).isEmpty()){
            changePosition(getButton4(), getButton7());
        }
    }//GEN-LAST:event_button4ActionPerformed

    private void button6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button6ActionPerformed
        // button 6 so pode mudar para o button 5,3 e 9
        if(getText(getButton5()).isEmpty()){
           changePosition(getButton6(), getButton5());
        }
        else if(getText(getButton3()).isEmpty()){
           changePosition(getButton6(),getButton3());
        }
        else if(getText(getButton9()).isEmpty()){
           changePosition(getButton6(), getButton9());
        }
    }//GEN-LAST:event_button6ActionPerformed

    private void button7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button7ActionPerformed
        // button 7 so pode mudar para o button 4 e 8
        if(getText(getButton4()).isEmpty()){
            changePosition(getButton7(), getButton4());
        }else if(getText(getButton8()).isEmpty()){
            changePosition(getButton7(),getButton8());
        }
    }//GEN-LAST:event_button7ActionPerformed

    private void button8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button8ActionPerformed
        // button 8 so pode mudar para o button 5,7 e 9
        if(getText(getButton5()).isEmpty()){
            changePosition(getButton8(), getButton5());
        }
        else if(getText(getButton7()).isEmpty()){
            changePosition(getButton8(),getButton7());
        }else if(getText(getButton9()).isEmpty()){
            changePosition(getButton8(),getButton9());
        }
    }//GEN-LAST:event_button8ActionPerformed

    private void button9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button9ActionPerformed
        // button 9 so pode mudar para o button 6 e 8
        if(getText(getButton6()).isEmpty()){
            changePosition(getButton9(), getButton6());
        }
        else if(getText(getButton8()).isEmpty()){
            changePosition(getButton9(),getButton8());
        }
    }//GEN-LAST:event_button9ActionPerformed

    private void startIAResolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_startIAResolverActionPerformed
        fillDataResponsible((DataResponsible)dataResponsible);
        service.resolverPuzzle();
    }//GEN-LAST:event_startIAResolverActionPerformed

    public void fillDataResponsible(DataResponsible dr)
    {
        String[][] values = {{getText(getButton1()),getText(getButton2()), getText(getButton3())},
                             {getText(getButton4()), getText(getButton5()), getText(getButton6())},
                             {getText(getButton7()), getText(getButton8()), getText(getButton9())}
                            };
        dr.setMovimentQuantity(0);
        dr.setValues(values);
    }

    private void resetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetActionPerformed
      String [][] inicialValues = Utils.getInitialValues();

      getButton1().setText(inicialValues[0][0]);
      getButton2().setText(inicialValues[0][1]);
      getButton3().setText(inicialValues[0][2]);
      getButton4().setText(inicialValues[1][0]);
      getButton5().setText(inicialValues[1][1]);
      getButton6().setText(inicialValues[1][2]);
      getButton7().setText(inicialValues[2][0]);
      getButton8().setText(inicialValues[2][1]);
      getButton9().setText(inicialValues[2][2]);
      getTotalMovimentos().setText("0");
    }//GEN-LAST:event_resetActionPerformed

    private void stopResolverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stopResolverActionPerformed
        if(service!=null){
            service.stopResolvePuzzle();
        }
    }//GEN-LAST:event_stopResolverActionPerformed
    
    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Main m= new Main();
                m.setVisible(true);
                m.setTitle("Game 8 Numeros Reverso");
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton button1;
    private javax.swing.JButton button2;
    private javax.swing.JButton button3;
    private javax.swing.JButton button4;
    private javax.swing.JButton button5;
    private javax.swing.JButton button6;
    private javax.swing.JButton button7;
    private javax.swing.JButton button8;
    private javax.swing.JButton button9;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenu menu;
    private javax.swing.JMenuItem reset;
    private javax.swing.JMenuItem startIAResolver;
    private javax.swing.JMenuItem stopResolver;
    private javax.swing.JLabel totalMovimentos;
    // End of variables declaration//GEN-END:variables

    public JButton getButton1() {
        return button1;
    }

    public void setButton1(JButton button1) {
        this.button1 = button1;
    }

    public JButton getButton2() {
        return button2;
    }

    public void setButton2(JButton button2) {
        this.button2 = button2;
    }

    public JButton getButton3() {
        return button3;
    }

    public void setButton3(JButton button3) {
        this.button3 = button3;
    }

    public JButton getButton4() {
        return button4;
    }

    public void setButton4(JButton button4) {
        this.button4 = button4;
    }

    public JButton getButton5() {
        return button5;
    }

    public void setButton5(JButton button5) {
        this.button5 = button5;
    }

    public JButton getButton6() {
        return button6;
    }

    public void setButton6(JButton button6) {
        this.button6 = button6;
    }

    public JButton getButton7() {
        return button7;
    }

    public void setButton7(JButton button7) {
        this.button7 = button7;
    }

    public JButton getButton8() {
        return button8;
    }

    public void setButton8(JButton button8) {
        this.button8 = button8;
    }

    public JButton getButton9() {
        return button9;
    }

    public void setButton9(JButton button9) {
        this.button9 = button9;
    }

    public JLabel getTotalMovimentos() {
        return totalMovimentos;
    }

    public void setTotalMovimentos(JLabel totalMovimentos) {
        this.totalMovimentos = totalMovimentos;
    }

   public List<JButton> getButtons()
   {
       List<JButton> buttons = new ArrayList<JButton>();
       buttons.add(getButton1());
       buttons.add(getButton2());
       buttons.add(getButton3());
       buttons.add(getButton4());
       buttons.add(getButton5());
       buttons.add(getButton6());
       buttons.add(getButton7());
       buttons.add(getButton8());
       buttons.add(getButton9());
       return buttons;
   }

   public void changePosition(JButton origem,JButton destino){
        String textDestino = getText(destino);
        destino.setText(getText(origem));
        origem.setText(textDestino);
   }

   public String getText(JButton button){
       return button.getText();
   }

    @Override
    public void update(String[][] values, int movementTotal) {
        getButton1().setText(values[0][0]);
        getButton2().setText(values[0][1]);
        getButton3().setText(values[0][2]);
        getButton4().setText(values[1][0]);
        getButton5().setText(values[1][1]);
        getButton6().setText(values[1][2]);
        getButton7().setText(values[2][0]);
        getButton8().setText(values[2][1]);
        getButton9().setText(values[2][2]);
        getTotalMovimentos().setText(String.valueOf(movementTotal));
    }

    public void endGameMessage() {
        Integer movimentos=Integer.valueOf(getTotalMovimentos().getText());
        if(movimentos<=10){
            JOptionPane.showMessageDialog(null, "Jogo Resolvido!!!\nObrigado pelo pequeno desafio");
        }else if(movimentos<=15){
            JOptionPane.showMessageDialog(null, "Jogo Resolvido!!!\nDesafio Interessante o seu.");
        }else if(movimentos<=20){
            JOptionPane.showMessageDialog(null, "Jogo Resolvido!!!\nJogo bom esse seu.");
        }else if(movimentos<=30){
            JOptionPane.showMessageDialog(null, "Jogo Resolvido!!!\nNossa esse jogo ta ficando dificil.");
        }
    }
}
